package com.example.JavaWebApi.model.querymodel.SystemQueryModel;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.JavaWebApi.core.mybatisplusextention.wrapperCreator.annotation.IgnoreValue;
import com.example.JavaWebApi.core.neo4j.Neo4jQueryWrapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: lvzhengcao_api
 * @description:
 * @author: 杨泽
 * @create: 2023-10-31 15:05
 */
@Data
public class OrderPageQuery extends PageQuery {
    @IgnoreValue
    @ApiModelProperty("排序字段")
    protected String field;
    @IgnoreValue
    @ApiModelProperty("是否正序")
    protected boolean asc=true;
    /**
     * Description: mp插件自动添加排序规则
     * @param queryWrapper
     * @return com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<T>
     * @author 杨泽
     * @date 2023/10/31 15:10
     */
    public <T> QueryWrapper<T> appendOrder(QueryWrapper<T> queryWrapper){
        return appendOrder(queryWrapper,null,true);
    }
    public <T> QueryWrapper<T> appendOrder(QueryWrapper<T> queryWrapper,String defaultField,boolean defaultAsc){
        if(StringUtils.isNotBlank(field)){
            if(asc){
                queryWrapper.orderByAsc(field);
            }else {
                queryWrapper.orderByDesc(field);
            }
        }else {
            if(StringUtils.isNotBlank(defaultField)){
                if(defaultAsc){
                    queryWrapper.orderByAsc(defaultField);
                }else {
                    queryWrapper.orderByDesc(defaultField);
                }
            }
        }
        return queryWrapper;
    }

    /**
     * Description: jpa生成sort
     * @return org.springframework.data.domain.Sort
     * @author 杨泽
     * @date 2023/10/31 15:15
     */
    public Sort toSort(){
        return toSort("",false);
    }
    public Sort toSort(String defaultField,boolean defaultAsc){
        Sort.Order order;
        if(StringUtils.isNotBlank(field)){
            if(asc){
                order=new Sort.Order(Sort.Direction.ASC,field);
            }else {
                order=new Sort.Order(Sort.Direction.DESC,field);
            }
            return Sort.by(order);
        }else {
            if(StringUtils.isNotBlank(defaultField)){
                if(defaultAsc){
                    order=new Sort.Order(Sort.Direction.ASC,defaultField);
                }else {
                    order=new Sort.Order(Sort.Direction.DESC,defaultField);
                }
                return Sort.by(order);
            }
        }
        return null;
    }

    @Override
    public Pageable toPageable() {
        return toPageable("",false);
    }

    public Pageable toPageable(String defaultField,boolean defaultAsc){
        Pageable pageable;
        Sort sort=toSort(defaultField,defaultAsc);
        if(sort!=null){
            pageable= PageRequest.of(page-1,limit,sort);
        }
        else {
            pageable= PageRequest.of(page-1,limit);
        }
        return pageable;
    }

    public <T extends OrderPageQuery> void copyOrder(T query){
        field=query.getField();
        asc=query.isAsc();
    }

}
